# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# Contributor: Nicolas Bats <sl1200mk2@gmail.com>
# Contributor: Karlson2k (Evgeny Grin) <k2k@narod.ru>
# Contributor: Marcin Sielski <marcin.sielski@motorolasolutions.com>

_realname=libmicrohttpd
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=0.9.77
pkgrel=1
pkgdesc="GNU libmicrohttpd is a small C library that is supposed to make it easy to run an HTTP server as part of another application (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32' 'clangarm64')
url="https://www.gnu.org/software/libmicrohttpd"
license=('spdx:LGPL-2.1-or-later')
makedepends=("${MINGW_PACKAGE_PREFIX}-autotools"
             "${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-pkgconf")
checkdepends=("${MINGW_PACKAGE_PREFIX}-curl")
depends=("${MINGW_PACKAGE_PREFIX}-gnutls")
source=("https://ftp.gnu.org/gnu/libmicrohttpd/${_realname}-${pkgver}.tar.gz"{,.sig})
validpgpkeys=('D8423BCB326C7907033929C7939E6BE1E29FC3CC'  # Christian Grothoff <christian@grothoff.org>
              'EA812DBEFA5A7EF17DA8F2C1460A317C3326D2AE') # Karlson2k (Evgeny Grin) <k2k@narod.ru>
sha256sums=('9e7023a151120060d2806a6ea4c13ca9933ece4eacfc5c9464d20edddb76b0a0'
            'SKIP')

prepare() {
  cd ${_realname}-${pkgver}
  autoreconf -fiv
}

build() {
  declare -a extra_config
  if check_option "debug" "y"; then
    extra_config+=("--enable-asserts")
  else
    extra_config+=("--disable-asserts")
  fi

  mkdir -p "${srcdir}/build-${MSYSTEM}-static" && cd "${srcdir}/build-${MSYSTEM}-static"

  CPPFLAGS+=" -DGNUTLS_INTERNAL_BUILD" \
  ../${_realname}-${pkgver}/configure \
    --prefix=${MINGW_PREFIX} \
    --build=${MINGW_CHOST} \
    --host=${MINGW_CHOST} \
    "${extra_config[@]}" \
    --disable-examples \
    --enable-https \
    --enable-static \
    --disable-shared

  make

  mkdir -p "${srcdir}/build-${MSYSTEM}-shared" && cd "${srcdir}/build-${MSYSTEM}-shared"

  ../${_realname}-${pkgver}/configure \
    --prefix=${MINGW_PREFIX} \
    --build=${MINGW_CHOST} \
    --host=${MINGW_CHOST} \
    "${extra_config[@]}" \
    --disable-examples \
    --enable-https \
    --enable-shared \
    --disable-static

  make
}

check() {
  cd "${srcdir}/build-${MSYSTEM}-static"
  make check || true

  cd "${srcdir}/build-${MSYSTEM}-shared"
  make check || true
}

package() {
  cd "${srcdir}/build-${MSYSTEM}-static"
  make DESTDIR="${pkgdir}" install

  cd "${srcdir}/build-${MSYSTEM}-shared"
  make DESTDIR="${pkgdir}" install

  install -Dm644 ${srcdir}/${_realname}-${pkgver}/COPYING ${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE
}
